﻿using System;
using System.Collections.Generic;
using Aierp.Entities;
using Aierp.SystemEntities.System;
using Aierp.Utility.Data;
using Chloe;

namespace Aierp.Repository
{
    public class RolesRepository: DbInstance,IRepository<Sys_Roles>
    {
        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="sortOrder"></param>
        /// <param name="limit"></param>
        /// <param name="offset"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public string GetList(int limit, int offset, string filter, string sortOrder)
        {
            if (!string.IsNullOrEmpty(filter))
            {
               return this.GetPageList<Sys_Roles, object>(limit, offset, c => c.RoleName.Contains(filter), c => c.Sortnum, sortOrder);
            }
            return this.GetPageList<Sys_Roles, object>(limit, offset, null, c => c.Sortnum, sortOrder);
        }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Sys_Roles Find(string key)
        {
            return this.FindBy<Sys_Roles>(c => c.RoleName == key);
        }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Sys_Roles FindById(int id)
        {
            return this.FindBy<Sys_Roles>(c => c.KeyId == id);
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public List<Sys_Roles> FindAll()
        {
           return this.Find<Sys_Roles>();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Add(Sys_Roles item)
        {
            var result = this.Insert<Sys_Roles>(item, c => c.RoleName==item.RoleName);
            return result.Item1 < 0 ? result.Item1 : result.Item2.KeyId;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Update(Sys_Roles item)
        {
            //先检查是否存在
            var entity = this.Find(item.RoleName);
            if (entity == null) return -2;

            ////更新
            Db.TrackEntity(item);//在上下文中跟踪实体
            if (entity.RoleName != "管理员") entity.RoleName = item.RoleName;//要判断角色是否为"管理员"，管理员角色名称不能更新
            entity.Sortnum = item.Sortnum;
            entity.Remark = item.Remark;
            entity.IsDefault = item.IsDefault;

            return Db.Update(entity);//这时只会更新被修改的字段
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key">角色id</param>
        /// <returns></returns>
        public int Remove(int key)
        {
            //要判断角色是否为"管理员"，管理员角色不能删除
            var result = this.FindById(key);

            if (result.RoleName == "管理员") return -9;

            //要判断角色在Sys_UserRoles表中是否已经被使用，已经使用的角色不能删除
            var role = this.FindBy<Sys_UserRoles>(c => c.RoleID == key);
            if (role != null) return -8;

            IDbSession dbSession = Db.Session;
            try
            {
                dbSession.BeginTransaction();
                this.Delete<Sys_Roles>(key);//这里要同步删除两个表
                this.Delete<Sys_RoleNavBtns>((c => c.RoleId == key));
                dbSession.CommitTransaction();
                return 1;
            }
            catch
            {
                dbSession.RollbackTransaction();
                return 0;
            }
        }

    }
}
